<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>
<style>
    .box {
        margin: 100px;
        width: 460px;
    }

    .box .btn {
        height: 40px;
        margin-bottom: 5px;
    }

    .box .btn input {
        float: left;
        width: 100px;
        text-align: center;
        height: 40px;
        margin-right: 20px;
        border: 1px solid #334455;
    }

    .box .btn input:nth-child(4) {
        margin-right: 0px;
    }

    .box .con {
        width: 460px;
        height: 400px;
        border: 1px solid #ccc;
        box-sizing: border-box;
        display: none;
    }

    .box .btn .active {
        background: orange;
    }
</style>

<body>
    <div class="box">
        <div class="btn">
            <input type="button" class="btnList active" value="体育">
            <input type="button" class="btnList" value="娱乐">
            <input type="button" class="btnList" value="新闻">
            <input type="button" class="btnList" value="教育">
        </div>
        <div class="con" style="display: block;">体育</div>
        <div class="con">娱乐</div>
        <div class="con">新闻</div>
        <div class="con">教育</div>
    </div>
</body>

</html>
<script>
    //获取按钮的元素
    var btn = document.getElementsByTagName('input');
    //获取下面选项卡的元素集合
    // var con = document.getElementsByTagName('div'); 这个是获取页面中所有的div元素，指向获取class名为con的这个元素

    // 通过class名来获取元素
    var con = document.getElementsByClassName('con');

    //循环给按钮添加事件
    for (var i = 0; i < btn.length; i++) {
        //给每一个按钮添加一个idx的属性，并且赋值为按钮的索引
        btn[i].idx = i;
        //给每个元素添加点击事件
        btn[i].onclick = function () {
            for (var j = 0; j < btn.length; j++) {
                btn[j].className = 'btnList';
                con[j].style.display = 'none';
            }
            //给当前点击的元素添加active，使用this来表示
            this.className = 'btnList active';
            //更改con状态
            con[this.idx].style.display = 'block';
        }
    }
</script>